package com.exercise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.exercise.common.ServiceException;
import com.exercise.dto.DishDto;
import com.exercise.entity.Dish;
import com.exercise.entity.DishFlavor;
import com.exercise.mapper.DishMapper;
import com.exercise.service.DishFlavorService;
import com.exercise.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 陈凯
 * @create 2023-03-07-11:52
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    @Transactional
    public void saveDishFlavor(DishDto dishDto){
        //把菜品的基本信息保存到dish表中
        this.save(dishDto);
        //把口味信息保存到dish-flavor表中
        Long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        //遍历flavors
        flavors = flavors.stream().map((item)->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        //保存口味
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getDishFlavor(Long id) {
        //通过Id得到菜品的信息
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        //拷贝
        BeanUtils.copyProperties(dish, dishDto);
        //通过菜品Id得到口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(dishFlavorList);
        return dishDto;
    }

    @Override
    public void UpdateDish(DishDto dishDto) {
        //保存菜品基本信息
        this.updateById(dishDto);
        //以菜品Id删除口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //再保存页面返回的口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 以Id删除菜品
     * @param ids
     */
    @Override
    public void deleteDish(List<Long> ids) {
        //判断菜品是否启售
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(Dish::getId, ids).eq(Dish::getStatus, 1);
        int count = this.count(dishLambdaQueryWrapper);
        if (count > 0){
            throw new ServiceException("菜品正在售卖，不能删除");
        }
        //可以删除，先删Dish表
        this.removeByIds(ids);
        //再删关联表Dish_Flavor
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
    }
}
